package hotgammon.domain;

import java.util.ArrayList;
import java.util.Iterator;
 
public class StandardBoard implements Board{
	int[] loc;

	public StandardBoard(){
		initialize();
		//Location x = Location.values()[10];
	}
	/** Sets the checkers up on the board */
	public void initialize(){
		loc = new int[28];
		loc[24] = -2;   //B1 RED
		loc[19] = 5;	//B6 BLACK
		loc[17] = 3;	//B8 BLACK
		loc[13] = -5;	//B12 RED
		loc[1] = 2;		//R1 BLACK
		loc[6] = -5;	//R6 RED
		loc[8] = -3;	//R8 RED
		loc[12] = 5;	//R12 BLACK
	}

	/** Resets all board checkers to their initial position */
	public void reset() {
		initialize();
	}
	
	/** Move one checker from one location to another.
	 * PRECONDITION: The move must be valid on the present board!
	 * @param from the location to move the checker from
	 * @param to the location to move the checker to
	 */
	public void move(Location from, Location to) {   //NEW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		int checker = loc[from.getIndex()]/Math.abs(loc[from.getIndex()]);
		loc[from.getIndex()] = loc[from.getIndex()] - checker;
		loc[to.getIndex()] = loc[to.getIndex()] + checker;
	}
	
	/** Get the colour of the checkers on a given location.
	 *  
	 * @param location the location on the board to check
	 * @return the color of the checkers on this location
	 */
	public Color getColor(Location location) {
		return Color.getColorFromNumerical(loc[location.getIndex()]);
	}

	/** Get the count of checkers of this location.
	 * @param location the location to inspect
	 * @return a integer value showing the number of checkers on this location.
	 */
	public int getCount(Location location) {
		return Math.abs(loc[location.getIndex()]);
	}

	/** Return an interator that will enumerate all Locations on the board.
	 * Each location is simply the location enumeration object, thus you must
	 * access the location's contents through the board interface.
	 * Example:
	 * 
	 * for (Location l: board) {
	 *  noOfCheckers += board.getCount(l);
	 * }
	 * 
	 * @return an iterator over locations on the board
	 */
	public Iterator<Location> iterator() {
		ArrayList<Location> list = new ArrayList<Location>(); 
		for(Location i: Location.values()){
			list.add(i);
		}
		return list.iterator();
	}



}
